package com.sztz.oss.aliyun;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.sztz.oss.OssConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * 阿里云OSS API 模板
 * @author yzy
 * @date 2022/10/29 15:40
 */
@AllArgsConstructor
@Slf4j
public class AliYunOssTemplate {
    /**
     * Minio客户端
     */
    OSS oss;
    /**
     * Oss配置类
     */
    OssConfig ossConfig;

    /**
     * 阿里云OSS API 模板
     * @param uploadFile
     * @author yzy
     * @date 2022/10/29 15:40
     */
    public Boolean upload(MultipartFile uploadFile){
        PutObjectRequest putObjectRequest = null;
        try {
            putObjectRequest = new PutObjectRequest(ossConfig.getDefaultBucketName(),uploadFile.getOriginalFilename(),new ByteArrayInputStream(uploadFile.getBytes()));
            oss.putObject(putObjectRequest);
        } catch (IOException e) {
            log.error(ExceptionUtil.stacktraceToString(e));
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
